#!/bin/sh
# set -v

PLATFORM=my-linux-arm-qt
SCRIPT_PATH=$(pwd)

# 修改源码包解压后的名称
MAJOR_NAME=qt-everywhere-opensource-src
MAJOR_NAME2=qt-everywhere-src

# 修改需要下载的源码前缀和后缀
OPENSRC_VER_PREFIX=5.15
OPENSRC_VER_SUFFIX=.12

# 添加 tslib 交叉编译的动态库文件和头文件路径
TSLIB_LIB=$1/lib
TSLIB_INC=$1/include

# 添加 alsa 交叉编译的动态库文件和头文件路径
ALSA_LIB=$1/qt/lib
ALSA_INC=$1/include

# 修改源码包解压后的名称
PACKAGE_NAME=${MAJOR_NAME}-${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}
PACKAGE_NAME2=${MAJOR_NAME2}-${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}

# 定义编译后安装--生成的文件, 文件夹位置路径
INSTALL_PATH=$1
#mkdir -p ${INSTALL_PATH}

# 添加交叉编译工具链路径
# export PATH=$PATH:$SCRIPT_PATH/../../toolchains/gcc_aarch64_11/bin # 将工具链添加到环境变量 仅本shell生效
# 脚本是mk 脚本调过来的 环境变量在mk里面已经 export过了这里可以不用再次export
CROSS_CHAIN_PREFIX=aarch64-none-linux-gnu

# 定义压缩包名称
COMPRESS_PACKAGE=${PACKAGE_NAME}.tar.xz

# 无需修改--自动组合下载地址
OPENSRC_VER=${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}

case ${OPENSRC_VER_PREFIX} in
    5.9 | 5.12 | 5.13 | 5.14 |5.15 )
        DOWNLOAD_LINK=https://download.qt.io/archive/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${COMPRESS_PACKAGE}
    ;;
    *)
        DOWNLOAD_LINK=http://download.qt.io/new_archive/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${COMPRESS_PACKAGE}
    ;;
esac

# 无需修改--自动组合平台路径
CONFIG_PATH=${SCRIPT_PATH}/${PACKAGE_NAME2}/qtbase/mkspecs/${PLATFORM}

# 无需修改--自动组合配置平台路径文件
CONFIG_FILE=${CONFIG_PATH}/qmake.conf

# 下载源码包
do_download_src () {
    echo "\033[1;33mstart download ${PACKAGE_NAME}...\033[0m"

    if [ ! -f "${COMPRESS_PACKAGE}" ];then
        if [ ! -d "${PACKAGE_NAME}" ];then
            wget -c ${DOWNLOAD_LINK}
        fi
    fi

    echo "\033[1;33mdone...\033[0m"
 }

# 解压源码包
do_tar_package () {
    echo "\033[1;33mstart unpacking the ${PACKAGE_NAME2} package ...\033[0m"
    if [ ! -d "${PACKAGE_NAME2}" ];then
        tar -xf ${COMPRESS_PACKAGE}
    fi
    echo "\033[1;33mdone...\033[0m"
    cd ${PACKAGE_NAME2}

    # 修复 5.11.3 版本的 bug
    if [ ${OPENSRC_VER_PREFIX}=="5.11" -a ${OPENSRC_VER_SUFFIX}==".3" ];then
        sed 's/asm volatile /asm /' -i qtscript/src/3rdparty/javascriptcore/JavaScriptCore/jit/JITStubs.cpp
    fi
}

# 安装依赖项
do_install_config_dependent () {
    sudo apt install python -y
    sudo apt install g++ make qt3d5-dev-tools -y
    sudo apt install qml-module-qtquick-xmllistmodel -y
    sudo apt install qml-module-qtquick-virtualkeyboard qml-module-qtquick-privatewidgets qml-module-qtquick-dialogs qml -y
    sudo apt install libqt53dquickscene2d5 libqt53dquickrender5libqt53dquickinput5 libqt53dquickextras5 libqt53dquickanimation5libqt53dquick5 -y
    sudo apt install qtdeclarative5-dev qml-module-qtwebengine qml-module-qtwebchannel qml-module-qtmultimedia qml-module-qtaudioengine -y
}

# 修改配置平台
do_config_before () {
    echo "\033[1;33mstart configure platform...\033[0m"

    if [ ! -d "${CONFIG_PATH}" ];then
        cp -a ${SCRIPT_PATH}/${PACKAGE_NAME2}/qtbase/mkspecs/linux-arm-gnueabi-g++ ${CONFIG_PATH}
    fi

    echo "#" > ${CONFIG_FILE}
    echo "# qmake configuration for building with arm-linux-gnueabi-g++" >>${CONFIG_FILE}
    echo "#" >> ${CONFIG_FILE}
    echo "" >> ${CONFIG_FILE}
    echo "MAKEFILE_GENERATOR = UNIX" >> ${CONFIG_FILE}
    echo "CONFIG += incremental" >> ${CONFIG_FILE}
    echo "QMAKE_INCREMENTAL_STYLE = sublib" >> ${CONFIG_FILE}
    echo "" >> ${CONFIG_FILE}
    echo "include(../common/linux.conf)" >> ${CONFIG_FILE}
    echo "include(../common/gcc-base-unix.conf)" >> ${CONFIG_FILE}
    echo "include(../common/g++-unix.conf)" >> ${CONFIG_FILE}
    echo "" >> ${CONFIG_FILE}
    echo "# modifications to g++.conf" >> ${CONFIG_FILE}
    echo "QMAKE_CC = ${CROSS_CHAIN_PREFIX}-gcc -lts" >> ${CONFIG_FILE}
    echo "QMAKE_CXX = ${CROSS_CHAIN_PREFIX}-g++ -lts" >> ${CONFIG_FILE}
    echo "QMAKE_LINK = ${CROSS_CHAIN_PREFIX}-g++ -lts" >> ${CONFIG_FILE}
    echo "QMAKE_LINK_SHLIB = ${CROSS_CHAIN_PREFIX}-g++ -lts" >> ${CONFIG_FILE}
    echo "" >> ${CONFIG_FILE}
    echo "# modifications to linux.conf" >> ${CONFIG_FILE}
    echo "QMAKE_AR = ${CROSS_CHAIN_PREFIX}-ar cqs" >> ${CONFIG_FILE}
    echo "QMAKE_OBJCOPY = ${CROSS_CHAIN_PREFIX}-objcopy" >> ${CONFIG_FILE}
    echo "QMAKE_NM = ${CROSS_CHAIN_PREFIX}-nm -P" >> ${CONFIG_FILE}
    echo "QMAKE_STRIP = ${CROSS_CHAIN_PREFIX}-strip" >> ${CONFIG_FILE}
    echo "load(qt_config)" >> ${CONFIG_FILE}
    echo "" >> ${CONFIG_FILE}
    echo "QMAKE_INCDIR=${TSLIB_INC}" >> ${CONFIG_FILE}
    echo "QMAKE_LIBDIR=${TSLIB_LIB}" >> ${CONFIG_FILE}

    cat ${CONFIG_FILE}
    echo "\033[1;33mdone...\033[0m"
}

# 配置选项
do_configure () {
    echo "\033[1;33mstart configure ${PACKAGE_NAME2}...\033[0m"

    export CC="${CROSS_CHAIN_PREFIX}-gcc"
    export CXX="${CROSS_CHAIN_PREFIX}-g++"

./configure \
 -prefix ${INSTALL_PATH} \
 -xplatform ${PLATFORM} \
 -release \
 -opensource \
 -confirm-license \
 -no-openssl \
 -no-opengl \
 -no-xcb \
 -no-eglfs \
 -no-compile-examples \
 -no-pkg-config \
 -no-iconv \
 -no-glib \
 -tslib \
 -I"${TSLIB_INC}" \
 -L"${TSLIB_LIB}" \
 -alsa \
 -I"${ALSA_INC}" \
 -L"${ALSA_LIB}" \

 echo "\033[1;33m configure done...\033[0m"
 }


# 编译并且安装
do_make_install () {
    echo "\033[1;33mstart make and install ${PACKAGE_NAME2} ...\033[0m"
    make -j8 && make install
    echo "\033[1;33m install done...\033[0m"
}

# 删除下载的文件
do_delete_file () {
    cd ${SCRIPT_PATH}
    if [ -f "${COMPRESS_PACKAGE}" ];then
    sudo rm -f ${COMPRESS_PACKAGE}
    fi
}

do_download_src
do_tar_package
#do_install_config_dependent
do_config_before
do_configure
do_make_install
# do_delete_file

exit $?